package com.hailiao.beidou.message.ui.page.search

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.d10ng.compose.ui.AppColor
import com.d10ng.compose.ui.AppText
import com.d10ng.compose.ui.PageTransitions
import com.d10ng.compose.ui.base.CellRow
import com.d10ng.compose.ui.navigation.NavBar
import com.d10ng.datelib.toDateStr
import com.hailiao.beidou.message.data.db.entity.MessageEntity
import com.hailiao.beidou.message.ui.view.ContactAvatar
import com.hailiao.beidou.message.utils.back
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph

/**
 * 搜索详情
 * @Author d10ng
 * @Date 2024/3/2 15:10
 */
@RootNavGraph
@Destination(
    style = PageTransitions::class,
    navArgsDelegate = SearchDetailScreenNavArgs::class
)
@Composable
fun SearchDetailScreen(
    model: SearchDetailScreenViewModel = viewModel()
) {
    val name by model.nameFlow.collectAsState()
    val messages by model.messagesFlow.collectAsState()

    SearchDetailScreenView(
        name = name,
        messages = messages,
        onClickItem = model::onClickItem
    )
}

@Composable
private fun SearchDetailScreenView(
    name: String = "",
    messages: List<MessageEntity> = listOf(),
    onClickItem: (MessageEntity) -> Unit = {}
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(AppColor.Neutral.bg)
            .navigationBarsPadding()
    ) {
        NavBar(
            title = name,
            titleAlignment = Alignment.CenterStart,
            border = true,
            onClickBack = { back() }
        )
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f)
        ) {
            items(messages, { it.id }) { item ->
                ItemCell(name = name, msg = item, onClick = { onClickItem(item) })
            }
        }
    }
}

@Composable
private fun ItemCell(
    name: String,
    msg: MessageEntity,
    onClick: () -> Unit = {}
) {
    CellRow(
        modifier = Modifier
            .background(Color.White)
            .clickable { onClick() },
        divider = true,
        contentPaddingValues = PaddingValues(horizontal = 19.dp, vertical = 12.dp)
    ) {
        ContactAvatar(name = name)
        Column(
            modifier = Modifier
                .padding(start = 15.dp)
                .weight(1f)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = name,
                    style = AppText.Normal.Title.medium,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier.weight(1f)
                )
                Text(
                    text = msg.createTime.toDateStr(),
                    style = AppText.Normal.Tips.mini,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )
            }
            Text(
                text = msg.content,
                style = AppText.Normal.Tips.mini,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        }
    }
}

@Preview
@Composable
private fun SearchDetailScreenPreview() {
    SearchDetailScreenView()
}